<?php


namespace app\common\service\companyIndustry\realtime;

use app\common\service\BaseService;
use app\common\service\MongoService;

/**
 * 循环泵运行组合（脱硫）
 */
class SulfurPumpService extends BaseService
{
    /**
     * （减排前）循环泵运行组合
     * @param integer $facilityId 设施ID
     * @return string
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-07-23
     */
    public function singleCompanySingleDrainSingleFacility($facilityId, $planNumber)
    {
        $mongo = new MongoService();
        //有效工作分钟数
        //$validNum = $mongo->beforeWorkingMinutes();

        //循环泵1
        $pump1Num    = $mongo->getSulfurPumpNumber($facilityId, $planNumber, 'circulating_pump1_witching_stateX', 1);//计算开的数量1是开
        $pump1Status = $this->getStatusNumber($pump1Num);
        //循环泵2
        $pump2Num    = $mongo->getSulfurPumpNumber($facilityId, $planNumber, 'circulating_pump2_witching_stateX', 1);
        $pump2Status = $this->getStatusNumber($pump2Num);
        //循环泵3
        $pump3Num    = $mongo->getSulfurPumpNumber($facilityId, $planNumber, 'circulating_pump3_witching_stateX', 1);
        $pump3Status = $this->getStatusNumber($pump3Num);
        //循环泵4
        $pump4Num    = $mongo->getSulfurPumpNumber($facilityId, $planNumber, 'circulating_pump4_witching_stateX', 1);
        $pump4Status = $this->getStatusNumber($pump4Num);
        //循环泵组合，显示开的数量
        $result = (int)$pump1Status + (int)$pump2Status + (int)$pump3Status + (int)$pump4Status;
        return $result;
    }

    /**
     * 获取状态
     * @param int $pump1Num 循环泵运行组合的数量
     * @param int $validNum 有效工作分钟数
     * @return string
     * @author hongwei 2019-07-23
     */
    public function getStatus($pump1Num, $validNum)
    {
        $result = '';
        if ($pump1Num == $validNum) {
            $result = '开';
        }
        if ($pump1Num == 0) {
            $result = '关';
        }
        if ($pump1Num != $validNum) {
            $result = '开关';
        }
        return $result;
    }

    /**
     * 获取状态
     * @param int $pump1Num 循环泵运行组合的数量
     * @return string
     * @author hongwei 2019-07-23
     */
    public function getStatusNumber($pump1Num)
    {
        $result = '';
        if ($pump1Num > 0) {
            $result = 1;
        } else {
            $result = 0;
        }
        return $result;
    }


    //循环泵的SQL
    public function mongoSql()
    {
        $sql = <<<EOT
db.company_industry_monitor_sulfur_minute.find({
    "circulating_pump1_witching_stateX": 1,
    facility_id: 3,
    "publish_time": {
        $gte: "2019-06-26 00:00:00",
        $lte: "2019-07-02 23:59:59",
        
    },
    "circulating_pump1_witching_stateX": 0,
    "export_smoke_dust": {
        $ne: - 1
    },
    "emissions": {
        $ne: - 1
    }, //排放量不为-1
    
}).count()
EOT;
        return $sql;

    }


    public function mongoSql2()
    {
        $sql = <<<EOT
db.company_industry_monitor_sulfur_minute.aggregate([
    {
        $match: {
            facility_id: 3,
            "publish_time": {
                $gte: "2019-06-26 00:00:00",
                $lte: "2019-07-02 23:59:59",
                
            },
            "circulating_pump1_witching_stateX": 0,
            "export_smoke_dust": {
                $ne: - 1
            },
            "emissions": {
                $ne: - 1
            }, //排放量不为-1
            
        }
    },
    {
        $group: {
            _id: null,
            count: {
                $sum: 1
            },
            
        }
    },
    
]);

EOT;

        return $sql;
    }
}